package com.mifengs.mall.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.mifengs.mall.R;
import com.mifengs.mall.util.UIUtil;

import java.util.ArrayList;
import java.util.List;


public class StepsViewIndicator extends View {

    private int THUMB_SIZE ;

    private Paint paint = new Paint();
    private Paint selectedPaint = new Paint();
    private Paint txtPaint = new Paint();
    private Paint selectedTxtPaint = new Paint();
    private Paint txtFailPaint = new Paint();
    private int mNumOfStep = 4;
    private float mLineHeight;
    private float mThumbRadius;
    private float mCircleRadius;
    private float mPadding;
    private int mSelectedColor = 0xFF15A7EA;
    private int mBarColor = 0xFFC1C1C1;
    private int mFailColor = 0xFFF94261;
    private boolean mIsStepfail;
    private float mCenterY;
    private float mLeftX;
    private float mLeftY;
    private float mRightX;
    private float mRightY;
    private float mDelta;
    private List<Float> mThumbContainerXPosition = new ArrayList<>();
    private int mCompletedPosition;
    private Bitmap mBmpSuccess;
    private Bitmap mBmpFail;
    private String[] mStepTexts = {"","","","","",""};
    private StepStatus mStepStatus = StepStatus.steping;



    public StepsViewIndicator(Context context) {
        this(context, null);
    }

    public StepsViewIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StepsViewIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.StepsViewIndicator);
        mNumOfStep = a.getInt(R.styleable.StepsViewIndicator_numOfSteps, mNumOfStep);
        a.recycle();
        init();
    }

    private void init() {
        THUMB_SIZE = UIUtil.dp2px(30);
        mLineHeight = 0.1f * THUMB_SIZE;
        mThumbRadius = 0.4f * THUMB_SIZE;
        mCircleRadius = 0.8f * mThumbRadius;
        mPadding = 0.8f * THUMB_SIZE;
        mBmpSuccess = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pass_ok);
        mBmpFail = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pass_fail);
    }

    public void setStepSize(int size) {
        mNumOfStep = size;
        invalidate();
    }


    public List<Float> getThumbContainerXPosition() {
        return mThumbContainerXPosition;
    }


    public void setStepTexts(String[] stepTexts) {
        this.mStepTexts = stepTexts;
        invalidate();
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterY = 0.4f * getHeight();
        mLeftX = mPadding;
        mLeftY = mCenterY - (mLineHeight / 2);
        mRightX = getWidth() - mPadding;
        mRightY = 0.4f * (getHeight() + mLineHeight);
        mDelta = (mRightX - mLeftX) / (mNumOfStep - 1);

        mThumbContainerXPosition.add(mLeftX);
        for (int i = 1; i < mNumOfStep - 1; i++) {
            mThumbContainerXPosition.add(mLeftX + (i * mDelta));
        }
        mThumbContainerXPosition.add(mRightX);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 200;
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        int height = THUMB_SIZE+UIUtil.dp2px(10);
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(width, height);
    }

    public void setCompletedPosition(int position,boolean isFail) {
        mCompletedPosition = position;
        mIsStepfail = isFail;
        invalidate();
    }

    public void setCompletedPosition(int position,StepStatus status) {
        mCompletedPosition = position;
        mStepStatus = status;
        invalidate();
    }

    public void reset() {
        setCompletedPosition(0,false);
    }

    public void setSelectedColor(int selectedColor) {
        this.mSelectedColor = selectedColor;
    }

    public void setBarColor(int barColor) {
        mBarColor = barColor;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.setDrawFilter(new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

        // Draw rect bounds
        paint.setAntiAlias(true);
        paint.setColor(mBarColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
       // paint.setShadowLayer(5f,0,0,mSelectedColor);


        selectedPaint.setAntiAlias(true);
        selectedPaint.setColor(mSelectedColor);
        selectedPaint.setStyle(Paint.Style.STROKE);
        selectedPaint.setStrokeWidth(2);
        selectedPaint.setStyle(Paint.Style.FILL);

        txtPaint.setColor(mBarColor);
        txtPaint.setStyle(Paint.Style.FILL);
        txtPaint.setTextSize(mCircleRadius);

        txtFailPaint.setColor(mFailColor);
        txtFailPaint.setStyle(Paint.Style.FILL);
        txtFailPaint.setTextSize(mCircleRadius);

        selectedTxtPaint.setColor(mSelectedColor);
        selectedTxtPaint.setStyle(Paint.Style.FILL);
        selectedTxtPaint.setTextSize(mCircleRadius);

        //画 过渡线
        for (int i = 0; i < mThumbContainerXPosition.size() - 1; i++) {
            final float pos = mThumbContainerXPosition.get(i);
            final float pos2 = mThumbContainerXPosition.get(i + 1);
            float radius = (mRightY - mLeftY)/2;
            float distance = (pos2-pos-2*mCircleRadius)/5;
            float firstx = pos+mCircleRadius+distance;
            float firsty = mLeftY+radius;
            DrawWayStatus drawWayStatus = DrawWayStatus.drawAllNormal;
            if(i<mCompletedPosition){
                drawWayStatus = DrawWayStatus.drawAllSelected;
            }else if(i==mCompletedPosition){
               // drawWayStatus = mIsStepfail?DrawWayStatus.drawAllNormal:DrawWayStatus.drawHalf;

                if(mStepStatus == StepStatus.stepSuccess){
                    drawWayStatus = DrawWayStatus.drawHalf;
                }else{
                    drawWayStatus = DrawWayStatus.drawAllNormal;
                }

            }else if(i>mCompletedPosition){
                drawWayStatus = DrawWayStatus.drawAllNormal;
            }
            drawWay(canvas,firstx,firsty,radius,distance,drawWayStatus);
        }

        // 画 圈圈
        for (int i = 0; i < mThumbContainerXPosition.size(); i++) {
            final float pos = mThumbContainerXPosition.get(i);
            float cRx = mThumbContainerXPosition.get(i);
            float cRy = mCenterY;
            Rect src = new Rect(0,0,getWidth(),getHeight());
            RectF de = new RectF(cRx-mCircleRadius,cRy-mCircleRadius,cRx+mCircleRadius,cRy+mCircleRadius);
            if(i<=mCompletedPosition){
                boolean isNowStep = (i==mCompletedPosition);
             //   canvas.drawBitmap((isNowStep&&mIsStepfail)?mBmpFail:mBmpSuccess,src,de,paint);
                if(!isNowStep){
                    canvas.drawBitmap(mBmpSuccess,src,de,paint);
                }else{
                    /*if (mIsStepfail){
                        canvas.drawBitmap(mBmpFail,src,de,paint);
                    }else{
                        canvas.drawCircle(pos, mCenterY, mCircleRadius, selectedPaint);
                    }*/
                    switch (mStepStatus){
                        case steping:
                            canvas.drawCircle(pos, mCenterY, mCircleRadius, selectedPaint);
                            break;
                        case stepFail:
                            canvas.drawBitmap(mBmpFail,src,de,paint);
                            break;
                        case stepSuccess:
                            canvas.drawBitmap(mBmpSuccess,src,de,paint);
                            break;
                    }

                }
            }else{
                canvas.drawCircle(pos, mCenterY, mCircleRadius, paint);
            }

        }
//        for (int i = 0; i < mThumbContainerXPosition.size(); i++) {
//            canvas.drawText((i+1)+"",mThumbContainerXPosition.get(i)-0.25f*mCircleRadius,mCenterY+0.25f*mCircleRadius,(i <= mCompletedPosition) ? selectedTxtPaint : txtPaint);
//        }
        //画文字
        for(int i = 0; i < mThumbContainerXPosition.size(); i++){
            String text = mStepTexts[i];
            Rect bound = new Rect();
            selectedTxtPaint.getTextBounds(text, 0, text.length(), bound);

            float cRx = mThumbContainerXPosition.get(i);
            float cRy = mCenterY;

            float tStartX = cRx-(bound.right-bound.left)/2;
            float tStartY = cRy+(bound.bottom-bound.top)+1.2f*mCircleRadius;

            if(i<mCompletedPosition){
                canvas.drawText(text,tStartX,tStartY,selectedTxtPaint);
            }else if(i==mCompletedPosition){
                //canvas.drawText(text,tStartX,tStartY,mIsStepfail?txtFailPaint:selectedTxtPaint);
                switch (mStepStatus){
                    case stepSuccess:
                        canvas.drawText(text,tStartX,tStartY,selectedTxtPaint);
                        break;
                    case stepFail:
                        canvas.drawText(text,tStartX,tStartY,txtFailPaint);
                        break;
                    default:
                        canvas.drawText(text,tStartX,tStartY,selectedTxtPaint);
                        break;
                }
            }else if(i>mCompletedPosition){
                canvas.drawText(text,tStartX,tStartY,txtPaint);
            }

        }
    }

    private void drawWay(Canvas canvas,float firstx,float firsty,float radius,float distance,DrawWayStatus status){
        switch (status){
            //一半灰 一半蓝
            case drawHalf:
                canvas.drawCircle(firstx,firsty,radius,selectedPaint);
                canvas.drawCircle(firstx+distance,firsty,radius,selectedPaint);
                canvas.drawCircle(firstx+distance*2,firsty,radius,paint);
                canvas.drawCircle(firstx+distance*3,firsty,radius,paint);
                break;
            // 全是灰色
            case drawAllNormal:
                canvas.drawCircle(firstx,firsty,radius,paint);
                canvas.drawCircle(firstx+distance,firsty,radius,paint);
                canvas.drawCircle(firstx+distance*2,firsty,radius,paint);
                canvas.drawCircle(firstx+distance*3,firsty,radius,paint);
                break;
            // 全是蓝色
            case drawAllSelected:
                canvas.drawCircle(firstx,firsty,radius,selectedPaint);
                canvas.drawCircle(firstx+distance,firsty,radius,selectedPaint);
                canvas.drawCircle(firstx+distance*2,firsty,radius,selectedPaint);
                canvas.drawCircle(firstx+distance*3,firsty,radius,selectedPaint);
                break;
        }
    }


    private enum DrawWayStatus{
        drawHalf,
        drawAllSelected,
        drawAllNormal
    }


    public enum StepStatus{
        steping,
        stepFail,
        stepSuccess
    }

}
